#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFileDialog>
#include <QTextCodec>
#include <iostream>
#include <memory>

#include <allheaders.h>
#include <D:/QtProject/tesseracte_lib/tesseract_x64-windows/include/tesseract/baseapi.h>      // tesseract提供的关于C++的接口
#include <D:/QtProject/tesseracte_lib/tesseract_x64-windows/include/tesseract/strngs.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Basic example
      char *outText;

      tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
      // Initialize tesseract-ocr with English, without specifying tessdata path
      if (api->Init("D:/QtProject/tesseracte_lib/tessdata", "chi_sim"))
      {
          fprintf(stderr, "Could not initialize tesseract.\n");
          exit(1);
      }

//      QTextCodec *pcode = QTextCodec::codecForName ("GB2312");

//      QString path = pcode->toUnicode("D:/图片/测试图片2.jpg");

      QString filename = QFileDialog::getOpenFileName(nullptr, tr("文件上传"), "", "");
      QTextCodec *code = QTextCodec::codecForName("GB2312");
      std::string str = code->fromUnicode(filename).data();

//      std::string str = path.toStdString();
      const char* ch = str.c_str();

      // Open input image with leptonica library
      Pix *image = pixRead(ch); //absolute path of file
      api->SetImage(image);

      // Get OCR result
      outText = api->GetUTF8Text();

    ui->textEdit->setText(outText);

//      QString strName = pcode->toUnicode(outText);
//      qDebug() << strName;

//      printf("OCR output:\n%s", outText);

      // Destroy used object and release memory
      api->End();
      delete [] outText;
      pixDestroy(&image);


}

MainWindow::~MainWindow()
{
    delete ui;
}
